package mybatisPlugin.config;

import cn.hutool.db.sql.SqlUtil;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.lang.reflect.Method;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author byron li
 * @date 2019/11/20 15:57
 */

@Intercepts(
        {
//                @Signature( type = StatementHandler.class,
//                method = "query",
//                args = {Statement.class, ResultHandler.class}),

                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class}),
                @Signature(type = Executor.class, method = "query", args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class, CacheKey.class, BoundSql.class})
        
        }
)
public class DataAuthenticationPlugin implements Interceptor {
    private final SqlUtil sqlUtil = new SqlUtil();

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object target = invocation.getTarget(); //被代理对象
        Method method = invocation.getMethod(); //代理方法
        Object[] args = invocation.getArgs(); //方法参数
        // do something . . . . . .  方法拦截前执行代码块

        return invocation.proceed();
    }
 
    @Override
    public Object plugin(Object target) {
        // 判断是否拦截这个类型对象（根据@Intercepts注解决定），然后决定是返回一个代理对象还是返回原对象。
        // 故我们在实现plugin方法时，要判断一下目标类型，如果是插件要拦截的对象时才执行Plugin.wrap方法，否则的话，直接返回目标本身。

    
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
//        sqlUtil.setProperties(properties);
        System.out.println(properties);
    }
}
